home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cenvid9.zip / FIND#.CMM < prev    next >
Text File  |  1994-03-04  |  3KB  |  64 lines

  1. //***************************************************************
  2. //*** Find#.cmm - CEnvi program to find the first number in a ***
  3. //*** ver.1       program's output.  This can be a standalone ***
  4. //***             program but is more often called from other ***
  5. //***             CEnvi utilities to work with program output ***
  6. //***************************************************************
  7.  
  8. #define  DEFAULT_INDEX  1
  9.  
  10. main(ArgCount,ArgStrings)
  11. {
  12.    Index = DEFAULT_INDEX
  13.    if ( ArgCount != 1
  14.      && ( ArgCount != 2  ||  0 == (Index = atoi(ArgStrings[1])) ) )
  15.       Instructions()
  16.    else {
  17.       HowManyFound = 0; // this will keep a list of how many numbers found so far
  18.       while ( (input = gets()) ) {
  19.          // get the next number in this line
  20.          while ( (input = strpbrk(input,"-0123456789")) ) {
  21.             if ( '-' == input[0] && !isdigit(input[1]) ) {
  22.                // this was a negative sign, but no numbers follow
  23.                input++
  24.             } else {
  25.                // convert these digits to a number
  26.                NumberList[HowManyFound] = atol(input)
  27.                if ( ++HowManyFound == Index )
  28.                      return(NUMBER = NumberList[HowManyFound-1])
  29.                // skip past all the digits text
  30.                input += 1 + strspn(input+1,"0123456789")
  31.             }
  32.          }
  33.       }
  34.       if ( Index < 0 ) {
  35.          // if enough numbers have been found, then find this offset from the end
  36.          if ( -Index <= HowManyFound ) {
  37.             return(NUMBER = NumberList[HowManyFound+Index])
  38.          }
  39.       }
  40.    }
  41.    // if did not return earlier, then number was not found
  42.    undefine(NUMBER)
  43.    return(0)
  44. }
  45.  
  46. Instructions()
  47. {
  48.    printf("\a\n")
  49.    printf("Find#.cmm - Read input for numbers. Set the NUMBER environment variable to the\n")
  50.    printf("            number found and return ERRORLEVEL to number found. 0 if not found.\n")
  51.    printf("\n")
  52.    printf("USAGE: CEnvi Find#.cmm [Index]\n")
  53.    printf("\n")
  54.    printf("Where Index tells which found number to return, where 1 is the first number\n")
  55.    printf("found, 2 is the seconds number, and so on; -1 is the last number found, -2 is\n")
  56.    printf("the second to last number, and so on.  If Index is not supplied then the\n")
  57.    printf("default is 1.  If the number is not found then NUMBER is cleared.\n")
  58.    printf("\n")
  59.    printf("Example: To find how many bytes are used the current directory:\n")
  60.    printf("             dir | cenvi Find#.cmm -2\n")
  61.    printf("\n")
  62. }
  63.  
  64.